"""add is_active to users

Revision ID: xxx
Revises: previous_revision_id
Create Date: 2024-03-27 23:30:00

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'xxx'  # 这里会自动生成
down_revision = 'previous_revision_id'  # 需要替换为你的上一个迁移版本ID
branch_labels = None
depends_on = None

def upgrade():
    # 添加 is_active 字段，默认值为 True
    op.add_column('users', sa.Column('is_active', sa.Boolean(), nullable=True, server_default='1'))
    
    # 将现有用户的 is_active 设置为 True
    op.execute("UPDATE users SET is_active = TRUE WHERE is_active IS NULL")
    
    # 将字段设为非空
    op.alter_column('users', 'is_active',
                    existing_type=sa.Boolean(),
                    nullable=False,
                    server_default='1')

def downgrade():
    op.drop_column('users', 'is_active') 